﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

using Game.Level;
using GameEditor;

namespace Game.Editor
{
    public partial class CampaignTreeControl : UserControl
    {
        public event EventHandler GameClose;
        public event EventHandler<LevelMilestoneEventArgs> GameMilestoneSelected;
        public event EventHandler<LevelEventArgs> GameLevelSelected;

        readonly CampaignTreeControlPersister _Persister;

        public CampainTreeLevelsItem _Root;
        public string _Filename;

        public CampaignTreeControl()
        {
            InitializeComponent();
            EnableButtons();

            _Persister = new CampaignTreeControlPersister(this);
        }

        public CampainTreeLevelsItem Root
        {
            get { return _Root; }
            set
            {
                _Root = value;
                this.treeView1.Items.Clear();
                this.treeView1.Items.Add(_Root);
            }
        }

        void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            Close();

            if (GameClose != null)
            {
                GameClose(this, new EventArgs());
            }
        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            _Persister.LoadGame();
            EnableButtons();
        }

        void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            _Persister.Save(_Root.GameLevels);
        }

        void ButtonNew_Click(object sender, RoutedEventArgs e)
        {
            _Root = new CampainTreeLevelsItem();
            this.treeView1.Items.Add(_Root);
        }

        void ButtonAddLevel_Click_1(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = this.treeView1.SelectedItem as TreeViewItem;
            if (item == null)
            {
                return;
            }

            GameLevel.Create(this._Root.GameLevels);
        }

        void ButtonRemoveLevel_Click(object sender, RoutedEventArgs e)
        {
            CampainTreeLevelItem item = this.treeView1.SelectedItem as CampainTreeLevelItem;
            if (item != null)
            {
                item.Remove();
            }
        }

        void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = this.treeView1.SelectedItem as TreeViewItem;
            if (item == null)
            {
                return;
            }

            CampainTreeLevelItem levelItem = item as CampainTreeLevelItem;
            if (levelItem != null)
            {
                if (GameLevelSelected != null)
                {
                    LevelEventArgs args = new LevelEventArgs() { GameLevel = levelItem.GameLevel };
                    GameLevelSelected(this, args);
                }
            }

            CampainTreeMilestoneItem itemMilestone = item as CampainTreeMilestoneItem;
            if (itemMilestone != null)
            {
                if (GameMilestoneSelected != null)
                {
                    LevelMilestoneEventArgs args = new LevelMilestoneEventArgs()
                    {
                        GameMilestone = itemMilestone.GameMilestone,
                        GameLevel = itemMilestone.GameLevel
                    };
                    GameMilestoneSelected(this, args);
                }
            }

            EnableButtons();
        }

        public void EnableButtons()
        {
            if (_Root != null)
            {
                this.ButtonClose.IsEnabled = true;
                this.ButtonSave.IsEnabled = true;
                this.ButtonNew.IsEnabled = false;
                this.Load.IsEnabled = false;
            }
            else
            {
                this.ButtonClose.IsEnabled = false;
                this.ButtonSave.IsEnabled = false;
                this.ButtonNew.IsEnabled = true;
                this.Load.IsEnabled = true;
            }

            TreeViewItem item = this.treeView1.SelectedItem as TreeViewItem;
            if (item != null)
            {
                if (item == _Root)
                {
                    this.ButtonAddLevel.IsEnabled = true;
                }
                else
                {
                    this.ButtonAddLevel.IsEnabled = false;
                }

                CampainTreeLevelItem itemLevel = item as CampainTreeLevelItem;
                if (itemLevel != null)
                {
                    this.ButtonRemoveLevel.IsEnabled = true;
                }
                else
                {
                    this.ButtonRemoveLevel.IsEnabled = false;
                }
            }
            else
            {
                this.ButtonAddLevel.IsEnabled = false;
                this.ButtonRemoveLevel.IsEnabled = false;
            }
        }

        public void Close()
        {
            this.treeView1.Items.Clear();
            this._Root = null;
            _Filename = null;
            EnableButtons();
        }

        public void CreateRoot()
        {
            _Root = new CampainTreeLevelsItem();
            this.treeView1.Items.Add(_Root);
        }

    }

    public class LevelMilestoneEventArgs : EventArgs
    {
        public GameLevel GameLevel { get; set; }

        public GameMilestone GameMilestone { get; set; }
    }

}
